package cappuccino.couldbook.ui.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cappuccino.couldbook.App;
import cappuccino.couldbook.R;
import cappuccino.couldbook.core.image.ImageReader;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.data.PhoneNumber;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.data.User;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/12.
 */
public class RecylerNumbersAdapter extends RecyclerView.Adapter<RecylerNumbersAdapter.NumberViewHolder> {

    List<PhoneNumber> numbers;
    Context context;
    int type;

    public RecylerNumbersAdapter(Context context, List<PhoneNumber> numbers,int type) {
        this.numbers = numbers;
        this.context = context;
        this.type = type;
    }

    @Override
    public NumberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NumberViewHolder holder;
        holder = new NumberViewHolder(LayoutInflater.from(App.getInstance().getApplicationContext()).inflate(R.layout.item_number_info_edit, parent, false),type);
        return holder;
    }

    @Override
    public void onBindViewHolder(NumberViewHolder holder, int position) {
        holder.setInfo(position);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return numbers.size();
    }

    class NumberViewHolder extends RecyclerView.ViewHolder {
        EditText tv_number;
        ImageView iv_photo;
        EditText tv_tag;
        View view;
        DrawerLayout drawer;
        ImageView open, close;
        Button edit, del;

        int status;


        String lastTag, lastNumber;

        public NumberViewHolder(View itemView, int type) {
            super(itemView);
            this.view = itemView;
            tv_number = (EditText) itemView.findViewById(R.id.tv_bar_number);
            tv_tag = (EditText) itemView.findViewById(R.id.tv_bar_tag);
            iv_photo = (ImageView) itemView.findViewById(R.id.iv_bar_photo);
            open = (ImageView) itemView.findViewById(R.id.iv_draw_open);
            if (type == 1) {
                drawer = (DrawerLayout) itemView.findViewById(R.id.drawer);
                close = (ImageView) itemView.findViewById(R.id.iv_draw_close);
                edit = (Button) itemView.findViewById(R.id.btn_draw_edit);
                del = (Button) itemView.findViewById(R.id.btn_draw_del);
                status = 1;
                View.OnClickListener listener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case R.id.iv_draw_open:
                                switch (status) {
                                    case 1:
                                        drawer.openDrawer(GravityCompat.END);
                                        break;
                                    case 2:
                                        tv_number.setEnabled(false);
                                        tv_tag.setEnabled(false);
                                        tv_number.clearFocus();
                                        tv_tag.clearFocus();
                                        status = 1;
                                        open.setImageResource(R.drawable.ic_keyboard_arrow_left_black_24dp);

                                        String tag = tv_tag.getText().toString();
                                        String number = tv_number.getText().toString();

                                        if (tag.equals(lastTag) && number.equals(lastNumber)) {
                                            Toast.makeText(context, "No change", Toast.LENGTH_SHORT).show();
                                        } else {

                                            NetApi api = Network.getNetApi();
                                            ;
                                            api.editNumber(numbers.get(getLayoutPosition()).id, tag, number).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Result<PhoneNumber>>() {
                                                @Override
                                                public void call(Result<PhoneNumber> result) {
                                                    switch (result.status) {
                                                        case 0:
                                                            Toast.makeText(context, "Fail, please check if the number exist", Toast.LENGTH_SHORT).show();
                                                            tv_tag.setText(lastTag);
                                                            tv_number.setText(lastNumber);
                                                            break;
                                                        case 1:
                                                            Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
                                                            break;
                                                    }
                                                }
                                            }, new Action1<Throwable>() {
                                                @Override
                                                public void call(Throwable throwable) {
                                                    Toast.makeText(context, "Fail, please check your network", Toast.LENGTH_SHORT).show();
                                                    tv_tag.setText(lastTag);
                                                    tv_number.setText(lastNumber);
                                                }
                                            });
                                        }
                                        break;
                                }
                                break;
                            case R.id.iv_draw_close:
                                drawer.closeDrawers();
                                break;
                            case R.id.btn_draw_del:
                                new AlertDialog.Builder(context).setMessage("是否确定删除号码").setNegativeButton("Cancel", null).setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        NetApi api = Network.getNetApi();
                                        api.delNumber(numbers.get(getLayoutPosition()).id).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Result<PhoneNumber>>() {
                                            @Override
                                            public void call(Result<PhoneNumber> result) {
                                                switch (result.status) {
                                                    case 0:
                                                        Toast.makeText(context, "Fail, please check if the number exist", Toast.LENGTH_SHORT).show();
                                                        break;
                                                    case 1:
                                                        Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
                                                        numbers.remove(getLayoutPosition());
                                                        notifyDataSetChanged();
                                                        break;
                                                }
                                            }
                                        }, new Action1<Throwable>() {
                                            @Override
                                            public void call(Throwable throwable) {
                                                Toast.makeText(context, "Fail, please check your network", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                        notifyDataSetChanged();
                                    }
                                }).show();
                                break;
                            case R.id.btn_draw_edit:
                                status = 2;
                                open.setImageResource(R.drawable.ic_done_black_24dp);
                                drawer.closeDrawers();
                                tv_number.setEnabled(true);
                                tv_tag.setEnabled(true);
                                tv_tag.requestFocus();

                                lastTag = tv_tag.getText().toString();
                                lastNumber = tv_number.getText().toString();

                                break;
                        }
                    }
                };
                open.setOnClickListener(listener);
                close.setOnClickListener(listener);
                edit.setOnClickListener(listener);
                del.setOnClickListener(listener);
            }else {
                open.setVisibility(View.INVISIBLE);
            }
        }


        public void setInfo(final int pos) {
            ImageReader.getReader(R.drawable.back).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<Bitmap>() {
                        @Override
                        public void call(Bitmap bitmap) {
                            // iv_photo.setImageBitmap(bitmap);
                            tv_tag.setText(numbers.get(pos).detail);
                            tv_number.setText(numbers.get(pos).number);
                        }
                    });
        }

    }

}
